//
//  HEMenuItemCell.m
//  HealthElderly
//
//  Created by 吴欧 on 2020/11/2.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEMenuItemCell.h"

@interface HEMenuItemCell ()

@property (nonatomic, assign) BOOL hasCreate;

@property (nonatomic, strong) NSMutableArray *btnArr;

@end

@implementation HEMenuItemCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        _btnArr = @[].mutableCopy;
    }
    return self;
}

- (void)setBrandArr:(NSMutableArray<HEBrandModel *> *)brandArr {

    _brandArr = brandArr;

    if (_brandArr.count == 0) {
        return;
    }
    if (_hasCreate) {
        for (int i = 0; i < _brandArr.count; i++) {
            HEBrandModel *model = self.brandArr[i];
            UIButton *btn = _btnArr[i];
            btn.selected = model.isSelect;
        }
        return;
    }

    _hasCreate = YES;

    [self layoutMultiLine: brandArr];
}


-(void)layoutMultiLine:(NSMutableArray<HEBrandModel *> *)brandArr {
    //多行布局 要考虑换行的问题
    
    CGFloat marginX = 10;  //按钮距离左边和右边的距离
    CGFloat marginY = 1;  //距离上下边缘距离
    CGFloat toTop = 0;  //按钮距离顶部的距离
    CGFloat gapX = 15;    //左右按钮之间的距离
    CGFloat gapY = 15;    //上下按钮之间的距离
    NSInteger col = 3;    //这里只布局5列
    
    CGFloat viewWidth = KScreenWidth;  //视图的宽度
    CGFloat viewHeight = KScreenHeight;  //视图的高度
    
    CGFloat itemWidth = (viewWidth - marginX *2 - (col - 1)*gapX)/col*1.0f;  //根据列数 和 按钮之间的间距 这些参数基本可以确定要平铺的按钮的宽度
    CGFloat itemHeight = 40;   //按钮的高度可以根据情况设定 这里设置为相等
    
    UIButton *last = nil;   //上一个按钮
    //准备工作完毕 既可以开始布局了
    for (int i = 0 ; i < brandArr.count; i++) {
        HEBrandModel *item = brandArr[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 4;
        button.tintColor = [UIColor clearColor];
        button.selected = item.isSelect;
        [button setTitle:brandArr[i].name forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [button setTitleColor:KWhiteColor forState:UIControlStateSelected];
        [button setBackgroundImage:[self imageWithColor:KHexColor(@"F8F8F8")] forState:UIControlStateNormal];
        [button setBackgroundImage:[self imageWithColor:CNavBgColor] forState:UIControlStateSelected];
        button.titleLabel.font = DYFont(16);
        button.tag = i;
        [button addTarget:self action:@selector(itemButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:button];
        
        //布局
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            
            //宽高是固定的，前面已经算好了
            make.width.mas_equalTo(itemWidth);
            make.height.mas_equalTo(itemHeight);
            
            //topTop距离顶部的距离，单行不用考虑太多，多行，还需要计算距离顶部的距离
            //计算距离顶部的距离 --- 根据换行
            CGFloat top = toTop + marginY + (i/col)*(itemHeight+gapY);
            make.top.mas_offset(top);
            if (!last || (i%col) == 0) {  //last为nil  或者(i%col) == 0 说明换行了 每行的第一个确定它距离左边边缘的距离
                make.left.mas_offset(marginX);
                
            }else{
                //第二个或者后面的按钮 距离前一个按钮右边的距离都是gap个单位
                make.left.mas_equalTo(last.mas_right).mas_offset(gapX);
            }
        }];
        last = button;
        [_btnArr addObject:button];
    }
}

- (void)itemButtonClick:(UIButton *)sender {
    NSInteger tag = sender.tag;
    
    HEBrandModel *model = self.brandArr[tag];
    model.isSelect = !model.isSelect;
    
    sender.selected = model.isSelect;
}

- (UIImage *)imageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}


@end
